<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small">添加权限</el-button>
        </div>
        <!-- row-key 表格开启树形结构 -->
        <el-table row-key="id" :data="list" border>
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{ row }">
              <el-button v-if="row.type === 1" type="text">添加</el-button>
              <el-button type="text">编辑</el-button>
              <el-button type="text" @click="hDel(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
  </div>
</template>
<script>
import { getPermissionList, delPermission } from '@/api/permission'
import { tranListToTreeList } from '@/utils'
export default {
  data() {
    return {
      list: []
    }
  },
  created() {
    this.loadPermissionList()
  },
  methods: {
    // 删除
    hDel(id) {
      try {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async() => {
            const res = await delPermission(id)
            this.$message.success(res.message)
            this.loadPermissionList()
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
          })
      } catch (error) {
        console.log(error)
      }
    },
    // 查询
    async loadPermissionList() {
      try {
        const res = await getPermissionList()
        console.log(res)
        this.list = tranListToTreeList(res.data)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
